﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;

namespace WordDef
{
    class Program
    {
        String  LISTFILE, OUTPUTFILE;
        static String APIKEY = "h9qptl7q1oqgszetaiwcno6qyazsynjfx94qubhw08";
        static void Main(string[] args)
        {
            if (args[0] == "-list")
            {
                wordlist(args[1], args[2]);
            }
            if (args[0] == "-single")
            {
                singleword(args[1]);
            }
        }
        static void singleword(string word)
        {
            string def, ant, syn;
            pulldef(APIKEY, word, out def);
            pullthe(APIKEY, word, out syn, out ant);
            Console.WriteLine("WORD: " + word);
            Console.WriteLine("FIRST DEFINITION: " + def);
            Console.WriteLine("FIRST SYN: " + syn);
            Console.WriteLine("FIRST ANT: " + ant);
            Console.ReadKey();
        }
        static void wordlist(string inputfile, string outputfile)
        {
            try
            {
                String[] word;
                String temp;
                TextReader read = new StreamReader(inputfile);
                TextWriter write = new StreamWriter(outputfile);
                temp = read.ReadToEnd();
                word = temp.Split(' ');
                for (int x = 0; x < word.Length - 1; x++)
                {
                    Console.WriteLine(word[x]);
                }
                
            }
            catch(IOException e)
            {
                Console.WriteLine(e);
            }
        }
        static void pulldef(string api ,string word, out string def)
        {
            string XMLD = "";
            string url = "http://api-pub.dictionary.com/v001?vid=" + api + "&q=" + word + "&type=define&site=dictionary";
            try
            {
                WebClient web = new WebClient();
                XMLD = web.DownloadString(url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            def = "";
            using (XmlReader reader = XmlReader.Create(new StringReader(XMLD)))
            {
                char[] o = { '(', ')' };
                reader.ReadToFollowing("defset");
                reader.ReadString();
                def = reader.ReadElementContentAsString();
                def = def.TrimStart(o);
                def = def.TrimEnd(o);
            }
            
        }
        static void pullthe(string api, string word, out string syn, out string ant)
        {
            string XMLT = "";
            string url = "http://api-pub.dictionary.com/v001?vid=" + api + "&q=" + word + "&type=define&site=thesaurus";
            syn = "";
            ant = "";
            try
            {
                WebClient web = new WebClient();
                XMLT = web.DownloadString(url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            using (XmlReader reader = XmlReader.Create(new StringReader(XMLT)))
            {
                int x;
                reader.ReadToFollowing("synonyms");
                syn = reader.ReadElementContentAsString();
                for (x = 0; x < syn.Length; x++)
                {
                    if (syn[x] == ',')
                        break;
                }
                syn = syn.Substring(0, x);
            }
        }
        static void pullhist(string api, string word, out string hist)
        {
            string XMLT = "";
            string url = "http://api-pub.dictionary.com/v001?vid=" + api + "&q=" + word + "&type=define&site=thesaurus";
            hist = "";
            try
            {
                WebClient web = new WebClient();
                XMLT = web.DownloadString(url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            using (XmlReader reader = XmlReader.Create(new StringReader(XMLT)))
            {
                int x;
                reader.ReadToFollowing("synonyms");
                hist = reader.ReadElementContentAsString();
                for (x = 0; x < hist.Length; x++)
                {
                    if (hist[x] == ',')
                        break;
                }
                hist = hist.Substring(0, x);
            }
        }
    }
}
